package com.example.shopproduct.controller;

import com.alibaba.fastjson.JSON;
import com.example.shopcommon.dao.entity.Product;
import com.example.shopproduct.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/get")
    public Product get(Long pid){
        Product product = productService.findByPId(pid);
        log.info("查询到商品:"+ JSON.toJSONString(product));
        return product;
    }

    //减少库存
    @GetMapping("/deduct")
    public String deduct(Long pid, int num) {
        try {
            productService.deduct(pid, num);
        } catch (Exception exx) {
            exx.printStackTrace();
            return "FAIL";
        }
        return "SUCCESS";
    }

}
